using System.Web.Mvc;

namespace KlearStorage // TODO: Shouldn't be this!?!
{
    public abstract class EntityController<T> : Controller
        where T : UniversalEntity, new()
    {
        protected readonly IPersister _persister = Persister.Instance;

        public virtual ActionResult Index()
        {
            return RedirectToAction("List"); // TODO: Show a friendly summary
        }

        public virtual ActionResult List()
        {
            var entities = _persister.SelectAll<T>();
            return View("List", entities);
        }

        public virtual ActionResult Details(System.Guid id)
        {
            var entity = _persister.SelectById<T>(id);
            return View(entity);
        }


        public virtual ActionResult Create()
        {
            return RedirectToAction("Edit", new { id = System.Guid.Empty });
        }

        public virtual ActionResult Edit(System.Guid id)
        {
            var entity = id == System.Guid.Empty
                ? new T()
                : _persister.SelectById<T>(id);
            return View(entity);
        }

        protected virtual void SetId(System.Guid id, T entity) // NOTE: Can also be called by a derived controller
        {
            if (id == System.Guid.Empty) // => Create
            {
                if (entity.Id == System.Guid.Empty) // => Action doesn't exclude it using [Bind] // TODO: Add warning?
                    entity.Id = System.Guid.NewGuid(); // The real id
            }
            else
                entity.Id = id; // The real id (different when Action doesn't exclude it using [Bind])
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public virtual ActionResult Edit(System.Guid id, [Bind(Exclude = "Id")] T entity)
        {
            SetId(id, entity);
            entity.Validate(ModelState);
            if (!ModelState.IsValid)
                return View(entity); // => Show validation error message

            try
            {
                if (id == System.Guid.Empty)
                    _persister.Insert(entity);
                else
                    _persister.Update(entity);

                return RedirectToAction("Index");
            }
            catch (System.Exception ex)
            {
                ModelState.AddModelError("Id", ex); // TODO: Error message doesn't appear!
                return View(entity); // => Show validation error message
            }
        }


        public virtual ActionResult Delete(System.Guid id)
        {
            var entity = _persister.SelectById<T>(id);
            return View(entity);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public virtual ActionResult Delete(T entity)
        {
            try
            {
                _persister.Delete(entity);

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}